import { getToken, setToken, removeToken } from '@/utils/auth'
import { getProfile, getuserphoto } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || null,
    userinfo: {}
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    removeUserinfo(state) {
      state.userinfo = {}
    },

    getuserinfo(state, userinfo) {
      state.userinfo = userinfo
    }
  },
  actions: {
    tuichu(context) {
      context.commit('removeToken')
      context.commit('removeUserinfo')
    },
    async getuserinfo(context) {
      const res = await getProfile()
      const res1 = await getuserphoto(res.data.userId)
      context.commit('getuserinfo', { ...res1.data, ...res.data })
    }
  }
}
